package Smt.ServerImp;

import Smt.BaseDao.SmtUserDao;
import Smt.BaseDaoImp.SmtUserDaoImp;
import Smt.Server.SmtUserServer;
import Smt.Util.SmtUser;

import java.util.ArrayList;

public class SmtUserServerImp implements SmtUserServer {
	SmtUserDao sud = new SmtUserDaoImp();
	private int yenum;
	private int SmtUserCount;

	public int getSmtUserCount() {
		return SmtUserCount;
	}

	public void setBillCount(int smtUserCount) {
		SmtUserCount = smtUserCount;
	}

	public int getYenum() {
		return yenum;
	}

	public void setYenum(int yenum) {
		this.yenum = yenum;
	}

	//查询用户
	public ArrayList<SmtUser> getSmtUser(String name) {
		Object[] o = {name};
		String sql = "select * from Super_User where name=?";
		return sud.getSmtUserList(o, sql);
	}

	//查询用户列表
	public ArrayList<SmtUser> getSmtUser() {
		Object[] o = null;
		String sql = "select * from Super_User ORDER by id";
		return sud.getSmtUserList(o, sql);
	}

	//查询用户权限
	public boolean rms(String name) {
		ArrayList<SmtUser> list = getSmtUser(name);
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getName().equals(name)) {
				if (list.get(i).getRms().equals("admin")) {
					return true;
				}
			}
		}
		return false;
	}

	//name账单总数
	public int SmtUserCount(String n) {
		return bd.SmtUserCount(n);
	}

	//条件信息列表
	public ArrayList<SmtUser> read(int y, int ye, String name) {//当前页，每页内容
		this.SmtUserCount = SmtUserCount(name);
		this.yenum = SmtUserCount / ye;//总页数
		if (SmtUserCount % ye != 0)
			this.yenum++;
		int tou = (y - 1) * ye + 1;//头记录
		int wei = y * ye;//尾记录
		Object[] o = {"%" + name + "%", tou, wei};
		String sql = "select * from (select id,name,sex,age,tel,adds,rms,password,@rownum:=@rownum+1 rn from Super_User,(select @rownum:=0) t where name like ? ORDER by id) a where a.rn>=? and a.rn<=?";
		return sud.getSmtUserList(o, sql);
	}

	//用户的增加
	public boolean add(SmtUser SmtUser) {
		String sql = "insert into Super_User(name,sex,age,tel,adds,rms,password) values(?,?,?,?,?,?,?)";
		Object[] o = {SmtUser.getName(), SmtUser.getSex(), SmtUser.getAge(), SmtUser.getTel(), SmtUser.getAdd(), SmtUser.getRms(), SmtUser.getPassword()};
		return bd.util(sql, o);
	}

	//用户的删除
	public boolean remove(int id) {
		Object[] o = {id};
		String sql = "delete from Super_User where id=?";
		return bd.util(sql, o);
	}

	//用户的更改
	public boolean update(SmtUser SmtUser) {

		String sql = "update Super_User set name=?,sex=?,age=?,tel=?,adds=?,rms=?,password=? where id=?";
		Object[] o = {SmtUser.getName(), SmtUser.getSex(), SmtUser.getAge(), SmtUser.getTel(), SmtUser.getAdd(), SmtUser.getRms(), SmtUser.getPassword(), SmtUser.getId()};
		return bd.util(sql, o);
	}

	//登陆验证
	public boolean login(String name, String password) {
		String sql = "select * from Super_User where name=? and password=?";
		Object[] o = {name, password};
		return bd.exist(sql, o);
	}

	//密码修改
	public boolean password_update(int id, String password) {
		String sql = "update Super_User set password=? where id=?";
		Object[] o = {password, id};
		return bd.util(sql, o);
	}
}
